import base64
import json
import mimetypes
import pika
import os
from apiapp import models
from apiapp import config

def get_image_base64(obj):
    with open(obj.path, 'rb') as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')

    content_type, _ = mimetypes.guess_type(obj.path)
    return f"data:{content_type};base64,{encoded_string}"



def sendToTelegram(user_id, message, date, tour_id):
    connection = pika.BlockingConnection(pika.ConnectionParameters(config.RABBIT))
    channel = connection.channel()
    channel.queue_declare(queue='messages')
    channel.basic_publish(exchange='',
                          routing_key='messages',
                          body=json.dumps({
                              'type': 'add',
                              'user_id': user_id,
                              'message': message,
                              'date': date,
                              'tourID': tour_id,
                          }),)
    connection.close()


def removeMessageFromTelegram(user_id, tour_id):
    connection = pika.BlockingConnection(pika.ConnectionParameters(config.RABBIT))
    channel = connection.channel()
    channel.queue_declare(queue='messages')
    channel.basic_publish(exchange='',
                          routing_key='messages',
                          body=json.dumps({
                              'type': 'delete',
                              'user_id': user_id,
                              'tourID': tour_id,
                          }),)
    connection.close()